const userModel = require('../model/userModel');
// 登录
module.exports.findUser = async function (user) {
    return await userModel.find(user);
}
// 查询
module.exports.find = async function (searchObj, page) {
    let { current, pageSize } = page;
    // 根据查询条件获取总数
    let total = await userModel.countDocuments(searchObj);
    // 根据查询条件获取具体数量的数据
    let data = await userModel.find(searchObj)
        .limit(pageSize)
        .skip((current - 1) * pageSize);
    return { current, pageSize, total, rows: data }
};
// 增加
module.exports.create = async function (user) {
    return await userModel.create(user);
};
// 删
module.exports.remove = async function (id) {
    return await userModel.remove({ _id: id });
};
// 改
module.exports.findById = async function (id) {
    return await userModel.findById({ _id: id });
};
module.exports.update = async function (id, user) {
    return await userModel.updateOne({ _id: id }, user);
}
// 验证用户名是否存在
module.exports.findByUsername = async function (username) {
    return await userModel.find({ username })
}
